package com.cmxh.shipper.controller;


import com.aliyun.ocr_api20210707.Client;
import com.aliyun.ocr_api20210707.models.RecognizeIdcardRequest;
import com.aliyun.ocr_api20210707.models.RecognizeIdcardResponse;
import com.aliyun.ocr_api20210707.models.RecognizeIdcardResponseBody;
import com.aliyun.oss.OSSClient;
import com.aliyun.teaopenapi.models.Config;
import com.cmxh.shipper.pojo.DriverRealname;
import com.cmxh.shipper.service.DriverRealnameService;
import com.cmxh.shipper.util.Code;
import com.cmxh.shipper.util.OSSUtil;
import com.cmxh.shipper.util.Result;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;



import java.util.Map;
import java.util.UUID;

/*
 *
 *实名认证Controller
 * */
@RestController
@RequestMapping("/driverRealname")
public class DriverRealnamecontroller {
    @Autowired
    private OSSUtil ossUtil;
    //身份证正反面的ocr识别
    @RequestMapping("/update")
    public Result update(MultipartFile file) throws Exception {
        System.out.println(file);
        String originalFilename = file.getOriginalFilename();
        String newname = UUID.randomUUID().toString().substring(24) + "-" + originalFilename;
        OSSClient xx = new OSSClient(ossUtil.getUrl(), ossUtil.getId(), ossUtil.getMiyao());
        xx.putObject(ossUtil.getTong(), newname, file.getInputStream());
        String url1 = ossUtil.getUrl();
        String[] split = url1.split("//");
        String url = split[0] + "//" + ossUtil.getTong() + "." + split[1] + "/" + newname;
        RecognizeIdcardRequest pp=new RecognizeIdcardRequest();
        pp.setUrl(url);
        Config config = new Config().setAccessKeyId(ossUtil.getId()).setAccessKeySecret(ossUtil.getMiyao()).setEndpoint("ocr-api.cn-hangzhou.aliyuncs.com");
        Client cc= new Client(config);
        RecognizeIdcardResponse ee=cc.recognizeIdcard(pp);
        RecognizeIdcardResponseBody body = ee.getBody();
        System.out.println(body.getRequestId());
        System.out.println(body.getCode());
        System.out.println(body.getData());
        System.out.println(body.getMessage());
        ObjectMapper oo = new ObjectMapper();
        Map map = oo.readValue(body.getData(), Map.class);
        System.out.println(map);
        Map data = (Map) map.get("data");
        System.out.println(data);
        Map face = (Map) data.get("face");
        System.out.println(face);
        if (face != null){
            Map o = (Map) face.get("data");
            System.out.println(o);
            DriverRealname driverRealname = new DriverRealname();
            driverRealname.setName(o.get("name").toString());
            driverRealname.setIdcard(o.get("idNumber").toString());
            driverRealname.setAddress(o.get("address").toString());
            driverRealname.setImagez(url);
            return new Result(Code.UPDATE_OK,driverRealname, "上传成功");
        }else {
            Map back = (Map) data.get("back");
            System.out.println(back);
            Map o = (Map) back.get("data");
            System.out.println(o);
            DriverRealname driverRealname = new DriverRealname();
            driverRealname.setQianf(o.get("issueAuthority").toString());
            driverRealname.setExpiration(o.get("validPeriod").toString());
            driverRealname.setImagef(url);
            return new Result(Code.UPDATE_OK, driverRealname, "上传成功");
        }
    }
    @Autowired
    private DriverRealnameService driverRealnameService;

    @RequestMapping("/add")
    public Result add(@RequestBody DriverRealname driverRealname) {
        System.out.println(driverRealname);
        int i = driverRealnameService.insert(driverRealname);
        if (i > 0) {
            return new Result(Code.SAVE_OK, "添加成功");
        } else {
            return new Result(Code.SAVE_ERR, "添加失败");
        }


    }

    @RequestMapping("/query/{id}")
    public Result query(@PathVariable Integer id) {
        DriverRealname driverRealname = driverRealnameService.selectByPrimaryKey(id);
        if (driverRealname != null) {
            return new Result(Code.GET_OK, driverRealname, "查询成功");
        } else {
            return new Result(Code.GET_ERR, "查询失败");
        }
    }



}